package com.sojson.project.controller.param.verify;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.sojson.enums.core.ECouponChargeMethod;
import com.sojson.enums.core.ECouponDateType;
import com.sojson.enums.core.ECouponGetMethod;
import com.sojson.enums.core.ECouponIsTogetherUse;
import com.sojson.enums.core.ECouponReducedMethod;
import com.sojson.enums.core.ECouponScopeGoods;
import com.sojson.enums.core.ECouponScopeModule;
import com.sojson.enums.core.ECouponScopeStore;
import com.sojson.enums.core.ECouponScopeUser;
import com.sojson.enums.core.ECouponStatus;
import com.sojson.enums.core.ECouponTypeScope;
import com.sojson.enums.core.EGeneralYesNo;
import com.sojson.project.entity.po.Coupon;
import com.sojson.project.entity.po.GoodSpecs;
import com.sojson.result.Result;
import com.sojson.util.ArithUtil;
import com.sojson.util.StringUtil;

/**
 * Coupon参数验证
 * 
 * @author liu
 * @date 2021-03-16
 */
public class CouponParamVerify {

    /**
     * 单条插入
     * 
     * @param coupon
     * @return
     * @throws SQLException
     */
    public static Result<Integer> insert(Coupon coupon) {
        int code = EGeneralYesNo.NO.getCode();
        if (StringUtil.isBlank(coupon.getName())) {
            return Result.fail("优惠券名称不能为空！");
        }
        if (StringUtil.isBlank(coupon.getType())) {
            return Result.fail("券类型不能为空！");
        }
        Integer number = coupon.getNumber();
        if (StringUtil.isBlank(number)) {
            return Result.fail("发放数量不能为空！");
        }
        if (number <= 0) {
            return Result.fail("发放数量必须大于0！");
        }
        Integer reducedMethod = coupon.getReducedMethod();
        if (StringUtil.isBlank(reducedMethod)) {
            return Result.fail("优惠方式不能为空！");
        }
        BigDecimal doorsill = coupon.getDoorsill();
        if (StringUtil.isBlank(doorsill)) {
            return Result.fail("使用门槛不能为空！");
        }
        String getMethod = coupon.getGetMethod();
        if (StringUtil.isBlank(getMethod)) {
            coupon.setGetMethod(ECouponGetMethod.CORE.getCode());
            getMethod = coupon.getGetMethod();
        }
        if (StringUtil.isBlank(coupon.getChargeMethod())) {
            coupon.setChargeMethod(ECouponChargeMethod.NO_PAY.getCode());
        }
        if (StringUtil.isBlank(coupon.getIsTogetherUse())) {
            coupon.setIsTogetherUse(ECouponIsTogetherUse.NO.getCode());
        }
        Integer dateType = coupon.getDateType();
        if (StringUtil.isBlank(dateType)) {
            coupon.setDateType(ECouponDateType.LIMITED_ALL.getCode());
            dateType = coupon.getDateType();
        }
        if (dateType == ECouponDateType.LIMITED_ALL.getCode()) {
            if (StringUtil.isBlank(coupon.getStartDate())) {
                return Result.fail("可用开始时间不能为空！");
            }
            if (StringUtil.isBlank(coupon.getEndDate())) {
                return Result.fail("可用结束时间不能为空！");
            }
        } else {
            return Result.fail("可用时间规则无此类型或暂未开放！");
        }
        Integer isBudgetDay = coupon.getIsBudgetDay();
        if (StringUtil.isBlank(isBudgetDay) || isBudgetDay == EGeneralYesNo.NO.getCode()) {
            coupon.setIsBudgetDay(code);
            coupon.setBudgetDay(null);
        } else if (isBudgetDay == EGeneralYesNo.YES.getCode()) {
            BigDecimal budgetDay = coupon.getBudgetDay();
            if (StringUtil.isBlank(budgetDay)) {
                return Result.fail("单天预算消耗上限金额不能为空！");
            }
            int intValue = budgetDay.intValue();
            if (intValue < 1) {
                return Result.fail("单天预算消耗上限金额必须大于0！");
            }
            coupon.setBudgetDay(ArithUtil.intPennyToBigDollar(intValue));
        }
        Integer isBudget = coupon.getIsBudget();
        if (StringUtil.isBlank(isBudget) || isBudget == EGeneralYesNo.NO.getCode()) {
            coupon.setIsBudget(code);
            coupon.setBudget(null);
        } else if (EGeneralYesNo.YES.getCode() == isBudget) {
            BigDecimal budget = coupon.getBudget();
            if (StringUtil.isBlank(budget)) {
                return Result.fail("总预算金额不能为空！");
            }
            int intValue = isBudget.intValue();
            if (intValue < 1) {
                return Result.fail("总预算金额必须大于0！");
            }
            coupon.setBudget(ArithUtil.intPennyToBigDollar(intValue));
        }
        int intValueDoorsill = doorsill.intValue();
        coupon.setDoorsill(ArithUtil.intPennyToBigDollar(intValueDoorsill));
        if (reducedMethod == ECouponReducedMethod.FIXED.getCode()) {
            BigDecimal price = coupon.getPrice();
            if (StringUtil.isBlank(price)) {
                return Result.fail("满减金额不能为空！");
            }
            int intValuePrice = price.intValue();
            if (intValueDoorsill < intValuePrice) {
                return Result.fail("满减金额必须小于等于使用门槛！");
            }
            if (intValuePrice < 1) {
                return Result.fail("满减金额必须大于0！");
            }
            coupon.setPrice(ArithUtil.intPennyToBigDollar(intValuePrice));
            coupon.setRandomStart(null);
            coupon.setRandomEnd(null);
            coupon.setDiscount(null);
            coupon.setIsDiscountCeiling(null);
            coupon.setDiscountCeiling(null);
        } else if (reducedMethod == ECouponReducedMethod.RANDOM.getCode()) {
            BigDecimal randomStart = coupon.getRandomStart();
            BigDecimal randomEnd = coupon.getRandomEnd();
            if (StringUtil.isBlank(randomStart)) {
                return Result.fail("随机立减开始区间不能为空！");
            }
            if (StringUtil.isBlank(randomEnd)) {
                return Result.fail("随机立减结束区间不能为空！");
            }
            int randomStartInt = randomStart.intValue();
            int randomEndInt = randomEnd.intValue();
            if (intValueDoorsill < randomEndInt) {
                return Result.fail("随机立减结束区间必须小于等于使用门槛！");
            }
            if (randomStartInt < 1) {
                return Result.fail("随机立减开始区间必须大于0！");
            }
            if (randomStartInt >= randomEndInt) {
                return Result.fail("随机立减开始区间必须小于随机立减结束区间！");
            }
            coupon.setRandomStart(ArithUtil.intPennyToBigDollar(randomStartInt));
            coupon.setRandomEnd(ArithUtil.intPennyToBigDollar(randomEndInt));
            coupon.setPrice(null);
            coupon.setDiscount(null);
            coupon.setIsDiscountCeiling(null);
            coupon.setDiscountCeiling(null);
        } else if (reducedMethod == ECouponReducedMethod.DISCOUNT.getCode()) {
            Integer discount = coupon.getDiscount();
            if (StringUtil.isBlank(discount)) {
                return Result.fail("折扣不能为空！");
            }
            if (discount < 1 || discount > 99) {
                return Result.fail("折扣必须大于0且小于100！");
            }
            Integer isDiscountCeiling = coupon.getIsDiscountCeiling();
            if (StringUtil.isBlank(isDiscountCeiling)) {
                coupon.setIsDiscountCeiling(code);
                coupon.setDiscountCeiling(null);
            } else if (EGeneralYesNo.YES.getCode() == isDiscountCeiling) {
                BigDecimal discountCeiling = coupon.getDiscountCeiling();
                if (StringUtil.isBlank(discountCeiling)) {
                    return Result.fail("折扣单笔优惠上限不能为空！");
                }
                int intValue = discountCeiling.intValue();
                if (intValue < 1) {
                    return Result.fail("折扣单笔优惠上限必须大于0！");
                }
                coupon.setDiscountCeiling(ArithUtil.intPennyToBigDollar(intValue));
            } else {
                coupon.setDiscountCeiling(null);
            }
            coupon.setPrice(null);
            coupon.setRandomStart(null);
            coupon.setRandomEnd(null);
        }
        Integer typeScope = coupon.getTypeScope();
        if (StringUtil.isBlank(typeScope)) {
            coupon.setTypeScope(ECouponTypeScope.ALL.getCode());
            typeScope = coupon.getTypeScope();
        }
        if (typeScope == ECouponTypeScope.ALL.getCode()) {
            coupon.setScopeModule(ECouponScopeModule.ALL.getCode());
            coupon.setScopeStore(ECouponScopeStore.ALL.getCode());
            coupon.setScopeGoods(ECouponScopeGoods.ALL.getCode());
            coupon.setUseModuleNo(null);
            coupon.setUseStoreNo(null);
            coupon.setUseGoodsNo(null);
            coupon.setModuleNos(null);
            coupon.setStoreNos(null);
            coupon.setGoodsNos(null);
        } else if (typeScope == ECouponTypeScope.CUSTOM_SCOPE.getCode()) {
            Integer scopeModule = coupon.getScopeModule();
            Integer scopeStore = coupon.getScopeStore();
            Integer scopeGoods = coupon.getScopeGoods();
            if (StringUtil.isBlank(scopeModule)) {
                coupon.setScopeModule(ECouponScopeModule.ALL.getCode());
                scopeModule = coupon.getScopeModule();
            }
            if (StringUtil.isBlank(scopeStore)) {
                coupon.setScopeStore(ECouponScopeStore.ALL.getCode());
                scopeStore = coupon.getScopeStore();
            }
            if (StringUtil.isBlank(scopeGoods)) {
                coupon.setScopeGoods(ECouponScopeGoods.ALL.getCode());
                scopeGoods = coupon.getScopeGoods();
            }

            if (scopeModule == ECouponScopeModule.ALL.getCode()) {
                coupon.setUseModuleNo(null);
                coupon.setModuleNos(null);
            } else if (scopeModule == ECouponScopeModule.APPOINT_SOME_YES.getCode()
                || scopeModule == ECouponScopeModule.APPOINT_SOME_NO.getCode()) {
                if (StringUtil.isBlankObject(coupon.getModuleNos())) {
                    return Result.fail("模块使用范围集合不能为空！");
                }
                coupon.setUseModuleNo(null);
            } else if (scopeModule == ECouponScopeModule.APPOINT.getCode()) {
                if (StringUtil.isBlankObject(coupon.getUseModuleNo())) {
                    return Result.fail("使用模块编号不能为空！");
                }
                coupon.setModuleNos(null);
            }
            if (scopeModule == ECouponScopeStore.ALL.getCode()) {
                coupon.setUseStoreNo(null);
                coupon.setStoreNos(null);
            } else if (scopeModule == ECouponScopeStore.APPOINT_SOME_YES.getCode()
                || scopeModule == ECouponScopeStore.APPOINT_SOME_NO.getCode()) {
                if (StringUtil.isBlankObject(coupon.getStoreNos())) {
                    return Result.fail("店铺使用范围集合不能为空！");
                }
                coupon.setUseStoreNo(null);
            } else if (scopeModule == ECouponScopeStore.APPOINT.getCode()) {
                if (StringUtil.isBlankObject(coupon.getUseStoreNo())) {
                    return Result.fail("使用店铺编号不能为空！");
                }
                coupon.setStoreNos(null);
            }
            if (scopeModule == ECouponScopeGoods.ALL.getCode()) {
                coupon.setUseGoodsNo(null);
                coupon.setGoodsNos(null);
            } else if (scopeModule == ECouponScopeGoods.APPOINT_SOME_YES.getCode()
                || scopeModule == ECouponScopeGoods.APPOINT_SOME_NO.getCode()) {
                if (StringUtil.isBlankObject(coupon.getGoodsNos())) {
                    return Result.fail("商品使用范围集合不能为空！");
                }
                coupon.setUseGoodsNo(null);
            } else if (scopeModule == ECouponScopeGoods.APPOINT.getCode()) {
                if (StringUtil.isBlankObject(coupon.getUseGoodsNo())) {
                    return Result.fail("使用商品编号不能为空！");
                }
                coupon.setGoodsNos(null);
            }
            return Result.fail("暂不支持自定义范围！");
        } else if (typeScope == ECouponTypeScope.APPOINT_MODULE.getCode()) {
            String useModuleNo = coupon.getUseModuleNo();
            if (StringUtil.isBlank(useModuleNo)) {
                return Result.fail("使用模块编号不能为空！");
            }
            coupon.setScopeModule(ECouponScopeModule.APPOINT.getCode());
            coupon.setScopeStore(ECouponScopeStore.ALL.getCode());
            coupon.setScopeGoods(ECouponScopeGoods.ALL.getCode());
            coupon.setUseStoreNo(null);
            coupon.setUseGoodsNo(null);
            coupon.setModuleNos(null);
            coupon.setStoreNos(null);
            coupon.setGoodsNos(null);
        } else if (typeScope == ECouponTypeScope.APPOINT_STORE.getCode()) {
            String useModuleNo = coupon.getUseModuleNo();
            if (StringUtil.isBlank(useModuleNo)) {
                return Result.fail("使用模块编号不能为空！");
            }
            String useStoreNo = coupon.getUseStoreNo();
            if (StringUtil.isBlank(useStoreNo)) {
                return Result.fail("使用店铺编号不能为空！");
            }
            coupon.setScopeModule(ECouponScopeModule.APPOINT.getCode());
            coupon.setScopeStore(ECouponScopeStore.APPOINT.getCode());
            coupon.setScopeGoods(ECouponScopeGoods.ALL.getCode());
            coupon.setUseGoodsNo(null);
            coupon.setModuleNos(null);
            coupon.setStoreNos(null);
            coupon.setGoodsNos(null);
        } else if (typeScope == ECouponTypeScope.APPOINT_GOODS.getCode()) {
            String useModuleNo = coupon.getUseModuleNo();
            if (StringUtil.isBlank(useModuleNo)) {
                return Result.fail("使用模块编号不能为空！");
            }
            String useStoreNo = coupon.getUseStoreNo();
            if (StringUtil.isBlank(useStoreNo)) {
                return Result.fail("使用店铺编号不能为空！");
            }
            String useGoodsNo = coupon.getUseGoodsNo();
            if (StringUtil.isBlank(useGoodsNo)) {
                return Result.fail("使用商品编号不能为空！");
            }
            coupon.setScopeModule(ECouponScopeModule.APPOINT.getCode());
            coupon.setScopeStore(ECouponScopeStore.APPOINT.getCode());
            coupon.setScopeGoods(ECouponScopeGoods.APPOINT.getCode());
            coupon.setModuleNos(null);
            coupon.setStoreNos(null);
            coupon.setGoodsNos(null);
        } else {
            return Result.fail("券使用范围类型不存在！");
        }
        Integer scopeUser = coupon.getScopeUser();
        String useUserNo = coupon.getUseUserNo();
        List<String> userNos = coupon.getUserNos();
        if (ECouponGetMethod.ACTIVITY.getCode().equals(getMethod)) {
            if (StringUtil.isNotBlank(useUserNo) || StringUtil.isNotBlank(userNos)) {
                return Result.fail("领取方式为活动领取的用户范围以活动的用户范围为准，请不要在设置用户范围！");
            }
            coupon.setScopeUser(ECouponScopeUser.ALL.getCode());
        } else if (ECouponGetMethod.MODULE.getCode().equals(getMethod)
            || ECouponGetMethod.STORE.getCode().equals(getMethod)
            || ECouponGetMethod.GOODS.getCode().equals(getMethod)) {
        } else {
        }
        if (StringUtil.isBlank(scopeUser)) {
            coupon.setScopeUser(ECouponScopeGoods.ALL.getCode());
            coupon.setUseUserNo(null);
            coupon.setUserNos(null);
        } else {
            if (scopeUser == ECouponScopeUser.APPOINT.getCode()) {
                if (StringUtil.isBlank(useUserNo)) {
                    return Result.fail("使用用户编号不能为空！");
                }
                coupon.setUserNos(null);
            } else if (scopeUser == ECouponScopeUser.APPOINT_SOME_NO.getCode()
                || scopeUser == ECouponScopeUser.APPOINT_SOME_YES.getCode()) {
                if (StringUtil.isBlank(userNos)) {
                    return Result.fail("用户使用范围集合不能为空！");
                }
                coupon.setUseUserNo(null);
            }
        }
        if (reducedMethod == ECouponReducedMethod.RANDOM.getCode()) {
            return Result.fail("此领取方式暂未开放！");
        }
        if (ECouponGetMethod.ACTIVITY.getCode().equals(getMethod)
            || ECouponGetMethod.SEND.getCode().equals(getMethod)) {
            return Result.fail("此领取方式暂未开放！");
        }

        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param coupon
     * @return
     * @throws SQLException
     */
    public static Result<Integer> updateById(Coupon coupon) {
        int code = EGeneralYesNo.NO.getCode();
        if (StringUtil.isBlank(coupon.getId())) {
            return Result.fail("ID不能为空！");
        }
        Integer status = coupon.getStatus();
        if (StringUtil.isNotBlank(status) && coupon.getStatus() == ECouponStatus.NO_LAUNCH.getCode()) {
            return Result.fail("状态不能为未投放！");
        }
        Integer dateType = coupon.getDateType();
        Date startDate = coupon.getStartDate();
        Date endDate = coupon.getEndDate();
        if (StringUtil.isNotBlankListOne(dateType, startDate, endDate)) {
            if (StringUtil.isBlank(dateType)) {
                coupon.setDateType(ECouponDateType.LIMITED_ALL.getCode());
                dateType = coupon.getDateType();
            }
            if (dateType == ECouponDateType.LIMITED_ALL.getCode()) {
                if (StringUtil.isBlank(coupon.getStartDate())) {
                    return Result.fail("可用开始时间不能为空！");
                }
                if (StringUtil.isBlank(coupon.getEndDate())) {
                    return Result.fail("可用结束时间不能为空！");
                }
            } else {
                return Result.fail("可用时间规则无此类型或暂未开放！");
            }
        }
        Integer isBudgetDay = coupon.getIsBudgetDay();
        BigDecimal budgetDay = coupon.getBudgetDay();
        if (StringUtil.isNotBlankListOne(isBudgetDay, budgetDay)) {
            if (StringUtil.isBlank(isBudgetDay) || isBudgetDay == EGeneralYesNo.NO.getCode()) {
                coupon.setIsBudgetDay(code);
                coupon.setBudgetDay(null);
            } else if (isBudgetDay == EGeneralYesNo.YES.getCode()) {
                if (StringUtil.isBlank(budgetDay)) {
                    return Result.fail("单天预算消耗上限金额不能为空！");
                }
                int intValue = budgetDay.intValue();
                if (intValue < 1) {
                    return Result.fail("单天预算消耗上限金额必须大于0！");
                }
                coupon.setBudgetDay(ArithUtil.intPennyToBigDollar(intValue));
            }
        }
        Integer isBudget = coupon.getIsBudget();
        BigDecimal budget = coupon.getBudget();
        if (StringUtil.isNotBlankListOne(isBudget, budget)) {
            if (StringUtil.isBlank(isBudget) || isBudget == EGeneralYesNo.NO.getCode()) {
                coupon.setIsBudget(code);
                coupon.setBudget(null);
            } else if (EGeneralYesNo.YES.getCode() == isBudget) {
                if (StringUtil.isBlank(budget)) {
                    return Result.fail("总预算金额不能为空！");
                }
                int intValue = isBudget.intValue();
                if (intValue < 1) {
                    return Result.fail("总预算金额必须大于0！");
                }
                coupon.setBudget(ArithUtil.intPennyToBigDollar(intValue));
            }
        }
        Integer reducedMethod = coupon.getReducedMethod();
        BigDecimal doorsill = coupon.getDoorsill();
        BigDecimal price = coupon.getPrice();
        BigDecimal randomStart = coupon.getRandomStart();
        BigDecimal randomEnd = coupon.getRandomEnd();
        Integer discount = coupon.getDiscount();
        Integer isDiscountCeiling = coupon.getIsDiscountCeiling();
        BigDecimal discountCeiling = coupon.getDiscountCeiling();
        if (StringUtil.isNotBlankListOne(reducedMethod, doorsill, price, randomStart, randomEnd, discount,
            isDiscountCeiling, discountCeiling)) {
            if (StringUtil.isBlank(reducedMethod)) {
                return Result.fail("优惠方式不能为空！");
            }
            if (StringUtil.isBlank(doorsill)) {
                return Result.fail("使用门槛不能为空！");
            }
            int intValueDoorsill = doorsill.intValue();
            coupon.setDoorsill(ArithUtil.intPennyToBigDollar(intValueDoorsill));
            if (reducedMethod == ECouponReducedMethod.FIXED.getCode()) {
                if (StringUtil.isBlank(price)) {
                    return Result.fail("满减金额不能为空！");
                }
                int intValuePrice = price.intValue();
                if (intValueDoorsill < intValuePrice) {
                    return Result.fail("满减金额必须小于等于使用门槛！");
                }
                if (intValuePrice < 1) {
                    return Result.fail("满减金额必须大于0！");
                }
                coupon.setPrice(ArithUtil.intPennyToBigDollar(intValuePrice));
                coupon.setRandomStart(null);
                coupon.setRandomEnd(null);
                coupon.setDiscount(null);
                coupon.setIsDiscountCeiling(null);
                coupon.setDiscountCeiling(null);
            } else if (reducedMethod == ECouponReducedMethod.RANDOM.getCode()) {
                if (StringUtil.isBlank(randomStart)) {
                    return Result.fail("随机立减开始区间不能为空！");
                }
                if (StringUtil.isBlank(randomEnd)) {
                    return Result.fail("随机立减结束区间不能为空！");
                }
                int randomStartInt = randomStart.intValue();
                int randomEndInt = randomEnd.intValue();
                if (intValueDoorsill < randomEndInt) {
                    return Result.fail("随机立减结束区间必须小于等于使用门槛！");
                }
                if (randomStartInt < 1) {
                    return Result.fail("随机立减开始区间必须大于0！");
                }
                if (randomStartInt >= randomEndInt) {
                    return Result.fail("随机立减开始区间必须小于随机立减结束区间！");
                }
                coupon.setRandomStart(ArithUtil.intPennyToBigDollar(randomStartInt));
                coupon.setRandomEnd(ArithUtil.intPennyToBigDollar(randomEndInt));
                coupon.setPrice(null);
                coupon.setDiscount(null);
                coupon.setIsDiscountCeiling(null);
                coupon.setDiscountCeiling(null);

                // TODO
                return Result.fail("此领取方式暂未开放！");
            } else if (reducedMethod == ECouponReducedMethod.DISCOUNT.getCode()) {
                if (StringUtil.isBlank(discount)) {
                    return Result.fail("折扣不能为空！");
                }
                if (discount < 1 || discount > 99) {
                    return Result.fail("折扣必须大于0且小于100！");
                }
                if (StringUtil.isBlank(isDiscountCeiling)) {
                    coupon.setIsDiscountCeiling(code);
                    coupon.setDiscountCeiling(null);
                } else if (EGeneralYesNo.YES.getCode() == isDiscountCeiling) {
                    if (StringUtil.isBlank(discountCeiling)) {
                        return Result.fail("折扣单笔优惠上限不能为空！");
                    }
                    int intValue = discountCeiling.intValue();
                    if (intValue < 1) {
                        return Result.fail("折扣单笔优惠上限必须大于0！");
                    }
                    coupon.setDiscountCeiling(ArithUtil.intPennyToBigDollar(intValue));
                } else {
                    coupon.setDiscountCeiling(null);
                }
                coupon.setPrice(null);
                coupon.setRandomStart(null);
                coupon.setRandomEnd(null);
            }
        }
        Integer typeScope = coupon.getTypeScope();
        Integer scopeModule = coupon.getScopeModule();
        Integer scopeStore = coupon.getScopeStore();
        Integer scopeGoods = coupon.getScopeGoods();
        String useModuleNo = coupon.getUseModuleNo();
        String useStoreNo = coupon.getUseStoreNo();
        String useGoodsNo = coupon.getUseGoodsNo();
        if (StringUtil.isNotBlankListOne(typeScope, scopeModule, scopeStore, scopeGoods, useModuleNo, useStoreNo,
            useGoodsNo)) {
            if (StringUtil.isBlank(typeScope)) {
                coupon.setTypeScope(ECouponTypeScope.ALL.getCode());
                typeScope = coupon.getTypeScope();
            }
            if (typeScope == ECouponTypeScope.ALL.getCode()) {
                coupon.setScopeModule(ECouponScopeModule.ALL.getCode());
                coupon.setScopeStore(ECouponScopeStore.ALL.getCode());
                coupon.setScopeGoods(ECouponScopeGoods.ALL.getCode());
                coupon.setUseModuleNo(null);
                coupon.setUseStoreNo(null);
                coupon.setUseGoodsNo(null);
            } else if (typeScope == ECouponTypeScope.CUSTOM_SCOPE.getCode()) {
                if (StringUtil.isBlank(scopeModule)) {
                    coupon.setScopeModule(ECouponScopeModule.ALL.getCode());
                    scopeModule = coupon.getScopeModule();
                }
                if (StringUtil.isBlank(scopeStore)) {
                    coupon.setScopeStore(ECouponScopeStore.ALL.getCode());
                    scopeStore = coupon.getScopeStore();
                }
                if (StringUtil.isBlank(scopeGoods)) {
                    coupon.setScopeGoods(ECouponScopeGoods.ALL.getCode());
                    scopeGoods = coupon.getScopeGoods();
                }

                if (scopeModule == ECouponScopeModule.ALL.getCode()) {
                    coupon.setUseModuleNo(null);
                } else if (scopeModule == ECouponScopeModule.APPOINT_SOME_YES.getCode()
                    || scopeModule == ECouponScopeModule.APPOINT_SOME_NO.getCode()) {
                    coupon.setUseModuleNo(null);
                } else if (scopeModule == ECouponScopeModule.APPOINT.getCode()) {
                    if (StringUtil.isBlankObject(coupon.getUseModuleNo())) {
                        return Result.fail("使用模块编号不能为空！");
                    }
                }
                if (scopeModule == ECouponScopeStore.ALL.getCode()) {
                    coupon.setUseStoreNo(null);
                } else if (scopeModule == ECouponScopeStore.APPOINT_SOME_YES.getCode()
                    || scopeModule == ECouponScopeStore.APPOINT_SOME_NO.getCode()) {
                    coupon.setUseStoreNo(null);
                } else if (scopeModule == ECouponScopeStore.APPOINT.getCode()) {
                    if (StringUtil.isBlankObject(coupon.getUseStoreNo())) {
                        return Result.fail("使用店铺编号不能为空！");
                    }
                }
                if (scopeModule == ECouponScopeGoods.ALL.getCode()) {
                    coupon.setUseGoodsNo(null);
                } else if (scopeModule == ECouponScopeGoods.APPOINT_SOME_YES.getCode()
                    || scopeModule == ECouponScopeGoods.APPOINT_SOME_NO.getCode()) {
                    coupon.setUseGoodsNo(null);
                } else if (scopeModule == ECouponScopeGoods.APPOINT.getCode()) {
                    if (StringUtil.isBlankObject(coupon.getUseGoodsNo())) {
                        return Result.fail("使用商品编号不能为空！");
                    }
                }
                return Result.fail("暂不支付自定义范围！");
            } else if (typeScope == ECouponTypeScope.APPOINT_MODULE.getCode()) {
                if (StringUtil.isBlank(useModuleNo)) {
                    return Result.fail("使用模块编号不能为空！");
                }
                coupon.setScopeModule(ECouponScopeModule.APPOINT.getCode());
                coupon.setScopeStore(ECouponScopeStore.ALL.getCode());
                coupon.setScopeGoods(ECouponScopeGoods.ALL.getCode());
                coupon.setUseStoreNo(null);
                coupon.setUseGoodsNo(null);
                coupon.setModuleNos(null);
                coupon.setStoreNos(null);
                coupon.setGoodsNos(null);
            } else if (typeScope == ECouponTypeScope.APPOINT_STORE.getCode()) {
                if (StringUtil.isBlank(useModuleNo)) {
                    return Result.fail("使用模块编号不能为空！");
                }
                if (StringUtil.isBlank(useStoreNo)) {
                    return Result.fail("使用店铺编号不能为空！");
                }
                coupon.setScopeModule(ECouponScopeModule.APPOINT.getCode());
                coupon.setScopeStore(ECouponScopeStore.APPOINT.getCode());
                coupon.setScopeGoods(ECouponScopeGoods.ALL.getCode());
                coupon.setUseGoodsNo(null);
                coupon.setModuleNos(null);
                coupon.setStoreNos(null);
                coupon.setGoodsNos(null);
            } else if (typeScope == ECouponTypeScope.APPOINT_GOODS.getCode()) {
                if (StringUtil.isBlank(useModuleNo)) {
                    return Result.fail("使用模块编号不能为空！");
                }
                if (StringUtil.isBlank(useStoreNo)) {
                    return Result.fail("使用店铺编号不能为空！");
                }
                if (StringUtil.isBlank(useGoodsNo)) {
                    return Result.fail("使用商品编号不能为空！");
                }
                coupon.setScopeModule(ECouponScopeModule.APPOINT.getCode());
                coupon.setScopeStore(ECouponScopeStore.APPOINT.getCode());
                coupon.setScopeGoods(ECouponScopeGoods.APPOINT.getCode());
                coupon.setModuleNos(null);
                coupon.setStoreNos(null);
                coupon.setGoodsNos(null);
            } else {
                return Result.fail("券使用范围类型不存在！");
            }
        }
        String getMethod = coupon.getGetMethod();
        Integer scopeUser = coupon.getScopeUser();
        String useUserNo = coupon.getUseUserNo();
        if (StringUtil.isNotBlankListOne(scopeUser, useUserNo)) {
            if (StringUtil.isBlank(getMethod)) {
                return Result.fail("领取方式不能为空！");
            }
            if (ECouponGetMethod.ACTIVITY.getCode().equals(getMethod)) {
                coupon.setScopeUser(ECouponScopeGoods.ALL.getCode());
                scopeUser = coupon.getScopeUser();
            }
            if (StringUtil.isBlank(scopeUser)) {
                coupon.setScopeUser(ECouponScopeGoods.ALL.getCode());
                coupon.setUseUserNo(null);
            } else {
                if (scopeUser == ECouponScopeUser.APPOINT.getCode()) {
                    if (StringUtil.isBlank(useUserNo)) {
                        return Result.fail("使用用户编号不能为空！");
                    }
                } else if (scopeUser == ECouponScopeUser.APPOINT_SOME_NO.getCode()
                    || scopeUser == ECouponScopeUser.APPOINT_SOME_YES.getCode()) {
                    coupon.setUseUserNo(null);
                }
            }
        }
        if (StringUtil.isNotBlank(getMethod)) {
            // TODO
            if (ECouponGetMethod.ACTIVITY.getCode().equals(getMethod)
                || ECouponGetMethod.SEND.getCode().equals(getMethod)) {
                return Result.fail("此领取方式暂未开放！");
            }
        }

        return Result.success();
    }

    /**
     * 通过id更新(局部更新)(启用,禁用)
     * 
     * @param coupon
     * @return
     * @throws SQLException
     */
    public static Result<?> updateStatusById(GoodSpecs goodSpecs) {
        if (StringUtil.isBlank(goodSpecs.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(goodSpecs.getStatus())) {
            return Result.fail("状态不能为空！");
        }

        return Result.success();
    }

    /**
     * 添加发放数量
     * 
     * @param coupon
     * @return
     * @throws SQLException
     */
    public static Result<Integer> addNumber(Coupon coupon) {
        if (StringUtil.isBlank(coupon.getId())) {
            return Result.fail("ID不能为空！");
        }
        if (StringUtil.isBlank(coupon.getNumber())) {
            return Result.fail("数量不能为空！");
        }

        return Result.success();
    }

    /**
     * 修改优惠券状态
     * 
     * @param coupon
     * @return
     * @throws SQLException
     */
    public static Result<Integer> updateStatus(Coupon coupon) {
        if (StringUtil.isBlank(coupon.getId())) {
            return Result.fail("ID不能为空！");
        }
        Integer status = coupon.getStatus();
        if (StringUtil.isBlank(status)) {
            return Result.fail("状态不能为空！");
        }
        if (coupon.getStatus() == ECouponStatus.NO_LAUNCH.getCode()) {
            return Result.fail("状态不能为未投放！");
        }

        return Result.success();
    }

}